package com.szosen.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;

import com.szosen.database.ChineseMedicineManage;
import com.szosen.jkzj.R;

import java.io.File;
import java.util.ArrayList;

public class ZYYBitmapUtil {
    /***
     * 中医体质的图片打印方法。
     */
    public static void createZhongyitizhiBitmap(final Context context,
                                                final ChineseMedicineManage manage, final String name,
                                                final Handler handler) {
        new BackGroundThread(new BackGroundThread.BackGroundTask() {

            @Override
            public void process() {
                int textSize = 34;
                Paint textPaint = new Paint();
                // textPaint.setStrokeWidth(3);
                textPaint.setDither(true);
                textPaint.setStyle(Paint.Style.FILL);
                textPaint.setTextSize(textSize);
                textPaint.setColor(Color.BLACK);
                System.gc();
                // 体检表1的画布，用来画好字。然后保存。
                Bitmap zhongyiyaoBitmap = getBitmapByResId(context,
                        R.raw.zhongyiyao1);
                Bitmap zhongyiyao = Bitmap.createBitmap(
                        zhongyiyaoBitmap.getWidth(),
                        zhongyiyaoBitmap.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(zhongyiyao);
                canvas.drawBitmap(zhongyiyaoBitmap, 0, 0, null);
                zhongyiyaoBitmap.recycle();
                Bitmap zhongyiyaoBitmap2 = getBitmapByResId(context,
                        R.raw.zhongyiyao2);
                Bitmap zhongyiyao2 = Bitmap.createBitmap(
                        zhongyiyaoBitmap2.getWidth(),
                        zhongyiyaoBitmap2.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas2 = new Canvas(zhongyiyao2);
                canvas2.drawBitmap(zhongyiyaoBitmap2, 0, 0, null);
                zhongyiyaoBitmap2.recycle();
                textPaint.setTextSize(textSize);
                canvas.drawText(name, 435, 129, textPaint);// 姓名
                String gou = "√";
                textPaint.setTextSize(60);
                // String title = SharedPreferenceUtil
                // .getHospitalName(context, "");
                int titleSize = 60;
                // textPaint.setTextSize(titleSize);
                // 画中医标题。
                // canvas.drawText(title, canvas.getWidth() / 2
                // - (titleSize * title.length() / 2), 60 * 2, textPaint);
                // canvas.drawText(userInfo.getSex(), 480, 354, textPaint);// 性别
                // canvas.drawText(userInfo.getYearold(), 754, 354,
                // textPaint);// 年龄
                // canvas.drawText(userInfo.getName(), 223, 372,
                // textPaint);//编号，暂时没有
                int defenX = 1337;
                textPaint.setTextSize(50);
                canvas.drawText(gou, getzyy1_x(manage.getEnergyFull()), 318,
                        textPaint);// 问题1
                canvas.drawText(gou, getzyy1_x(manage.getEasyWeary()), 371,
                        textPaint);// 问题2
                canvas.drawText(gou, getzyy1_x(manage.getEasyPant()), 434,
                        textPaint);// 问题3
                canvas.drawText(gou, getzyy1_x(manage.getVoiceWeak()), 499,
                        textPaint);// 问题4
                canvas.drawText(gou, getzyy1_x(manage.getMoodiness()), 559,
                        textPaint);// 问题5
                canvas.drawText(gou, getzyy1_x(manage.getNervous()), 616,
                        textPaint);// 问题6
                canvas.drawText(gou, getzyy1_x(manage.getLoneliness()), 681,
                        textPaint);// 问题7
                canvas.drawText(gou, getzyy1_x(manage.getEasyScare()), 743,
                        textPaint);// 问题8
                canvas.drawText(gou, getzyy1_x(manage.getOverweight()), 792,
                        textPaint);// 问题9
                canvas.drawText(gou, getzyy1_x(manage.getEyeDry()), 878,
                        textPaint);// 问题10
                canvas.drawText(gou, getzyy1_x(manage.getFootFearCold()), 933,
                        textPaint);// 问题11
                canvas.drawText(gou, getzyy1_x(manage.getBackFearCold()), 997,
                        textPaint);// 问题12
                canvas.drawText(gou, getzyy1_x(manage.getFearCold()), 1064,
                        textPaint);// 问题13
                canvas.drawText(gou, getzyy1_x(manage.getCold()), 1120,
                        textPaint);// 问题14
                canvas.drawText(gou, getzyy1_x(manage.getRhinobyon()), 1202,
                        textPaint);// 问题15
                canvas.drawText(gou, getzyy1_x(manage.getMouthGreasy()), 1262,
                        textPaint);// 问题16
                canvas.drawText(gou, getzyy1_x(manage.getAllergy()), 1329,
                        textPaint);// 问题17
                canvas.drawText(gou, getzyy1_x(manage.getSkinUrticaria()),
                        1429, textPaint);// 问题18
                canvas.drawText(gou, getzyy1_x(manage.getSkinBleeding()), 1492,
                        textPaint);// 问题19
                defenX = 1414;
                canvas2.drawText(gou, getzyy2_x(manage.getSkinRed()), 90,
                        textPaint);// 问题20
                canvas2.drawText(gou, getzyy2_x(manage.getSkinDry()), 151,
                        textPaint);// 问题21
                canvas2.drawText(gou, getzyy2_x(manage.getLimbsNumb()), 214,
                        textPaint);// 问题22
                canvas2.drawText(gou, getzyy2_x(manage.getFaceGreasy()), 273,
                        textPaint);// 问题23
                canvas2.drawText(gou, getzyy2_x(manage.getFaceDim()), 334,
                        textPaint);// 问题24
                canvas2.drawText(gou, getzyy2_x(manage.getSkinEczema()), 396,
                        textPaint);// 问题25
                canvas2.drawText(gou, getzyy2_x(manage.getMouthDry()), 457,
                        textPaint);// 问题26
                canvas2.drawText(gou, getzyy2_x(manage.getBitterTaste()), 519,
                        textPaint);// 问题27
                canvas2.drawText(gou, getzyy2_x(manage.getBellyLarge()), 519,
                        textPaint);// 问题28
                canvas2.drawText(gou, getzyy2_x(manage.getFearCool()), 585,
                        textPaint);// 问题29
                canvas2.drawText(gou, getzyy2_x(manage.getStoolStiction()),
                        742, textPaint);// 问题30
                canvas2.drawText(gou, getzyy2_x(manage.getStoolDry()), 826,
                        textPaint);// 问题31
                canvas2.drawText(gou, getzyy2_x(manage.getFurStodgily()), 888,
                        textPaint);// 问题32
                canvas2.drawText(gou, getzyy2_x(manage.getStasisPurple()),
                        1007, textPaint);// 问题33
                int defenY = 1140;
                textPaint.setTextSize(textSize);
                canvas2.drawText(manage.getScore1(), 366, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore2(), 573, defenY, textPaint);// 体质2
                canvas2.drawText(manage.getScore3(), 773, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore4(), 980, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore5(), 1160, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore6(), 1347, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore7(), 1591, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore8(), 1819, defenY, textPaint);// 体质1
                canvas2.drawText(manage.getScore9(), 2046, defenY, textPaint);// 体质1
                drawTizhiLeixing(canvas2, manage, textPaint);
                String date = manage.getReportDate();
                // FORMAT = "yyyy-MM-dd HH:mm:ss";
                if (!TextUtils.isEmpty(date) && date.length() >= 10) {
                    String year = date.substring(0, 4);
                    String month = date.substring(5, 7);
                    String day = date.substring(8, 10);
                    int dateY = 1502;
                    canvas2.drawText(year, 563, dateY, textPaint);// 年
                    canvas2.drawText(month, 702, dateY, textPaint);// 月
                    canvas2.drawText(day, 787, dateY, textPaint);// 日
                }
                String basePath = FileSaveUtil.getPrintPath_ZYTZBS() + File.separator + name;
                FileSaveUtil.saveBitmap(zhongyiyao, basePath, "zhongyiyao1.png");
                FileSaveUtil.saveBitmap(zhongyiyao2, basePath, "zhongyiyao2.png");
                zhongyiyao.recycle();
                zhongyiyao2.recycle();
                // 发送消息取消对话框
                Intent intent = new Intent();
                intent.setAction("android.intent.action.SEND_MULTIPLE");
                // intent.setDataAndType(Uri.fromFile(file), "image*//*");
                intent.setType("image/*");
                ArrayList<Uri> imageUris = new ArrayList<Uri>();
                File file = new File(basePath,
                        "zhongyiyao1.png");
                File file2 = new File(basePath,
                        "zhongyiyao2.png");
                imageUris.add(Uri.fromFile(file));
                imageUris.add(Uri.fromFile(file2));
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
                        imageUris);
                context.startActivity(intent);
            }
        }).start();
    }

    public static void drawTizhiLeixing(Canvas canvas2,
                                        ChineseMedicineManage manage, Paint textPaint) {
        int shi_y = 1180;
        int qingxiangshi_y = 1217;
        String gou = "√";
        int PianX = 30;
        String tizhi1 = manage.getPhysiqueIdentify1();
        if ("1".equals(tizhi1)) {
            canvas2.drawText(gou, 366 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi1)) {
            canvas2.drawText(gou, 366 + PianX, qingxiangshi_y, textPaint);// 体质1
        }

        String tizhi2 = manage.getPhysiqueIdentify2();
        if ("1".equals(tizhi2)) {
            canvas2.drawText(gou, 573 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi2)) {
            canvas2.drawText(gou, 573 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi3 = manage.getPhysiqueIdentify3();
        if ("1".equals(tizhi3)) {
            canvas2.drawText(gou, 773 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi3)) {
            canvas2.drawText(gou, 773 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi4 = manage.getPhysiqueIdentify4();
        if ("1".equals(tizhi4)) {
            canvas2.drawText(gou, 980 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi4)) {
            canvas2.drawText(gou, 980 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi5 = manage.getPhysiqueIdentify5();
        if ("1".equals(tizhi5)) {
            canvas2.drawText(gou, 1160 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi5)) {
            canvas2.drawText(gou, 1160 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi6 = manage.getPhysiqueIdentify6();
        if ("1".equals(tizhi6)) {
            canvas2.drawText(gou, 1347 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi6)) {
            canvas2.drawText(gou, 1347 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi7 = manage.getPhysiqueIdentify7();
        if ("1".equals(tizhi7)) {
            canvas2.drawText(gou, 1591 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi7)) {
            canvas2.drawText(gou, 1591 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi8 = manage.getPhysiqueIdentify8();
        if ("1".equals(tizhi8)) {
            canvas2.drawText(gou, 1819 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi8)) {
            canvas2.drawText(gou, 1819 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
        String tizhi9 = manage.getPhysiqueIdentify9();
        if ("1".equals(tizhi9)) {
            canvas2.drawText(gou, 2046 + PianX, shi_y, textPaint);// 体质1
        } else if ("2".equals(tizhi9)) {
            canvas2.drawText(gou, 2046 + PianX, qingxiangshi_y, textPaint);// 体质1
        }
    }

    /***
     * 返回是x ，根据传进来的12345个问题，返回对应的x值。用来画勾
     *
     * @return
     */
    public static int getzyy1_x(String str) {
        int result = 0;
        switch (str) {
            case "1":
                result = 1118;
                break;
            case "2":
                result = 1334;
                break;
            case "3":
                result = 1570;
                break;
            case "4":
                result = 1800;
                break;
            case "5":
                result = 2010;
                break;
            default:
                break;
        }
        return result;
    }

    /***
     * 返回是x ，根据传进来的12345个问题，返回对应的x值。用来画勾
     *
     * @return
     */
    public static int getzyy2_x(String str) {
        int result = 0;
        switch (str) {
            case "1":
                result = 1123;
                break;
            case "2":
                result = 1337;
                break;
            case "3":
                result = 1575;
                break;
            case "4":
                result = 1800;
                break;
            case "5":
                result = 2010;
                break;
            default:
                break;
        }
        return result;
    }

    public static Bitmap getBitmapByResId(Context context, int resId) {
        Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(),
                resId);
        return bitmap1;
    }

    // 将原来的11 22 33 对应起来，11转1 22转2 放到中医药上传的类对象中
    private static String getTizhiLeixing(String leixing) {
        if ("1".equals(leixing)) {
            return "是";
        } else if ("2".equals(leixing)) {
            return "倾向是";
        } else {
            return "否";
        }
    }
}
